package com.ziji;
/*
 * encryption 加密
 * Decrypt 解密
 * */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

public class FileTest
{
	private static int length= 100;
	private static int start= 20;
	private static int end = 60;
	public static void main(String[] args)
	{
		System.out.println("=文件的加密和解密练习=");
		/*创建文件路径*/
		/*相对路径*/
		String s = "docs\\a.jpg";
		File targetFile = new File(s );
		File selectFile = new File("docs\\a_q.jpg");
		File generateFile = new File("docs\\g.jpg");
		/*文件加密*/
		encryptionFile(targetFile ,selectFile);
//		/*文件解密*/
//		decryptFile(selectFile,generateFile);
	}
	/*文件解密*/
	private static void decryptFile(File generateFile , File selectFile)
	{
		/*解密文件*/
		BufferedInputStream br = null ;
		/*生成全新的文件*/
		BufferedOutputStream bo = null;
		try
		{
			/*读取所需要解密的文件
			 *	 注意要和加密的时候底的变量一致 
			 */
			br = new BufferedInputStream(new FileInputStream(generateFile));
			/*写出破解后的文件*/
			bo = new BufferedOutputStream(new FileOutputStream(selectFile));
			
			byte b [] = new byte[length];
			int len = 0 ;
			while((len= br.read(b))!=-1)
			{
				//System.out.println(len+"=="+Arrays.toString(b));
				if(len == length)
				{
					bo.write(b, start, end);
				}else
				{
					bo.write(b, 0, len);
				}
			}
			bo.flush();
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}finally
		{
			try
			{
				if(br != null)
				{
					br.close();
					br = null ;
				}
				if(bo != null)
				{
					bo.close();
					bo = null ;
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	
	}
	/*文件加密*/
	private static void encryptionFile(File targetFile,File generateFile)
	{
		/*先读取一张图片进行加密*/
		BufferedInputStream br = null ;
		BufferedOutputStream bp = null ;
		try
		{
			/*读入原始文件文件*/
			br = new BufferedInputStream(new FileInputStream(targetFile));
			/*写出加密后的文件*/
			bp = new BufferedOutputStream(new FileOutputStream(generateFile));
			
			/*生成随机数*/
			Random random = new Random();
			
			/*准备一个容器存储文件*/
			byte [] b = new byte[length];
			/*记录容器中装的什么*/
			int len = 0 ;
			while((len = br.read(b,start,end))!=-1)
			{
				/*随机生成左边的的字节数*/
				for (int i = 0; i < start; i++)
				{
					byte by =(byte)(random.nextInt(256)-128);
					b[i] = by ; 
				}
				/*随机生成右边的字节数*/
				for (int i =start+end+1 ; i <  length; i++)
				{
					byte by =(byte)(random.nextInt(256)-128);
					b[i] = by ; 
				}
				System.out.println(len+"=="+Arrays.toString(b));
				if(len == end)
				{
					bp.write(b);
				}else
				{
					
					bp.write(b, start, len);
				}
			}
			/*写文件的时候要记得关水管*/
			bp.flush();
		}catch (IOException e)
		{
			e.printStackTrace();
		}finally
		{
			try
			{
				if(br !=null)
				{
					br.close();
					br = null ;
				}
				if(bp != null )
				{
					bp.close();
					bp = null ;
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		
	}
}